package com.liao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liao.bean.Module;
import com.liao.bean.TreeNodeVo;
import com.liao.bean.result.Msg;
import com.liao.bean.result.PageResult;
import com.liao.dao.ModuleDao;
import com.liao.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ModuleServiceImpl implements ModuleService {

    @Autowired
    private ModuleDao moduleDao;

    @Override
    public List<Module> queryModuleListHierarchyByAccount(String account) throws Exception {
        return moduleDao.queryModuleListHierarchyByAccount(account);
    }

    @Override
    public PageResult<Module> queryModuleListByPage(Integer currentPage, Integer pageSize, String moduleName) throws Exception {
        PageHelper.startPage(currentPage,pageSize);
        //查询所有菜单
//        List<Module> moduleList = moduleDao.selectModuleList();
        List<Module> moduleList = moduleDao.selectModuleListLikeModuleName(moduleName);
        PageInfo<Module> pageInfo = new PageInfo<>(moduleList);
        return new PageResult<>(0, Msg.MODULE_QUERY_SUCCESS,pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public List<Module> queryFirstModuleList() throws Exception {
        return moduleDao.selectFirstModuleList();
    }

    @Override
    public List<Module> queryChildModuleListByParentModule(String parentModule) throws Exception {
        return moduleDao.selectChildModuleListByParentModule(parentModule);
    }

    @Override
    public List<TreeNodeVo> queryTreeNodeListHierarchy() throws Exception {
        return moduleDao.selectTreeNodeListHierarchy();
    }

    @Override
    public List<TreeNodeVo> queryThirdTreeNodeListByRoleId(String roleId) throws Exception {
        return moduleDao.selectThirdTreeNodeListByRoleId(roleId);
    }

    @Override
    public void deleteGrantsByRoleId(String roleId) throws Exception {
        moduleDao.deleteGrantsByRoleId(roleId);
    }

    @Override
    public void grant(String roleId, String moduleId) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("roleId",roleId);
        map.put("moduleId",moduleId);
        moduleDao.insertGrant(map);
    }

    @Override
    public String queryModuleByFinallyId(String parent_module) throws Exception {
        return moduleDao.selectModuleByFinallyId(parent_module);
    }

    @Override
    public void addFirstModule(String moduleId, String title, String parent_module) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("moduleId",moduleId);
        map.put("title",title);
        map.put("parent_module",parent_module);
        moduleDao.addFirstModule(map);
    }

    @Override
    public boolean queryModuleByTitle(String title, String parent_module) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("title",title);
        map.put("parent_module",parent_module);
        Module module = moduleDao.selectModuleByTitle(map);
        if (null != module){
            //已经存在
            return true;
        }else {
            //不存在
            return false;
        }

    }

    @Override
    public void addThirdModule(String moduleId, String title, String parent_module, String href) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("moduleId",moduleId);
        map.put("title",title);
        map.put("parent_module",parent_module);
        map.put("href",href);
        moduleDao.addThirdModule(map);
    }

    @Override
    public void editModule(String moduleId, String title, String href) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("moduleId",moduleId);
        map.put("title",title);
        map.put("href",href);
        moduleDao.updateModule(map);
    }

    @Override
    public void deleteModule(String moduleId) throws Exception {
        moduleDao.deleteModule(moduleId);
    }


}
